package com.cn.yang.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.production.ProductModel;
import com.cn.yang.service.ProductService;
import com.cn.yang.service.impl.ProductServiceImpl;
import com.cn.yang.util.PageUtil;
import com.cn.yang.util.SortUserUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/product/findProduct")
public class FindProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
        Integer pageNumber = Integer.valueOf(req.getParameter("pageNumber"));
        // String sortClassifyName = req.getParameter("sortClassifyName");
        String sortId = req.getParameter("sortId");
        String sortClassifyName = req.getParameter("sortProductClassify");
        String sortPrice = req.getParameter("sortPrice");
        String sortAverageprice = req.getParameter("sortAverageprice");
        ProductService productService = new ProductServiceImpl();
        SortUserUtil sortUserUtil = new SortUserUtil();

        // sortUserUtil.setSortClassifyName(sortClassifyName);
        sortUserUtil.setSortId(sortId);
        sortUserUtil.setSortAverageprice(sortAverageprice);
        sortUserUtil.setSortPrice(sortPrice);
        sortUserUtil.setSortClassifyName(sortClassifyName);
        ProductModel model = new ProductModel();
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPageNumber(pageNumber);
        pageUtil.setPageSize(pageSize);
        model.setPageUtil(pageUtil);
        model.setSortUserUtil(sortUserUtil);
        List<ProductModel> list = productService.findProduct(model);
        int count = productService.countProduct();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
}
